package com.cheng.example.geektime.designpatterns.j46.v1;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * @author chengshuai
 * @version 1.0
 * @date 2020/6/10 11:23
 */


/**
 *
 * 这种情况创建对象遇到构造器参数过程情况怎么解决？
 *
 * 解决：使用长参数列表的构造方法
 * 新的问题：构造函数的参数列表会变得很长，代码在可读性和易用性上都会变差
 */

@Data
public class ResourcePoolConfig {
    private static final int DEFAULT_MAX_TOTAL = 8;
    private static final int DEFAULT_MAX_IDLE = 8;
    private static final int DEFAULT_MIN_IDLE = 0;

    private String name;
    private int maxTotal = DEFAULT_MAX_TOTAL;
    private int maxIdle = DEFAULT_MAX_IDLE;
    private int minIdle = DEFAULT_MIN_IDLE;

    // 资源名称、最大总资源数量、最大空闲资源数量、最小空闲资源数量
    public ResourcePoolConfig(String name, Integer maxTotal, Integer maxIdle, Integer minIdle) {


        if (StringUtils.isBlank(name)) {
            throw new IllegalArgumentException("name should not be empty.");
        }
        this.name = name;

        if (maxTotal != null) {
            if (maxTotal <= 0) {
                throw new IllegalArgumentException("maxTotal should be positive.");
            }
            this.maxTotal = maxTotal;
        }

        if (maxIdle != null) {
            if (maxIdle < 0) {
                throw new IllegalArgumentException("maxIdle should not be negative.");
            }
            this.maxIdle = maxIdle;
        }

        if (minIdle != null) {
            if (minIdle < 0) {
                throw new IllegalArgumentException("minIdle should not be negative.");
            }
            this.minIdle = minIdle;
        }
    }
    //...省略getter方法...

    public static void main(String[] args) {
        // 参数太多，导致可读性差、参数可能传递错误
        ResourcePoolConfig config = new ResourcePoolConfig("pool", 100, 20, 40);

        // ResourcePoolConfig config = new ResourcePoolConfig("dbconnectionpool", 16, null, 8, null, false , true, 10, 20，false， true);


    }
}